clear all

use "${source}ACS2017-2021.dta" ,clear 


* Classify birthcountry World-Bank classificaton (high,middle,low income country)
* The classification can be accessed at the page https://datahelpdesk.worldbank.org/knowledgebase/articles/906519-world-bank-country-and-lending-groups by clicking on the link historical classification by income in XLSX format (we use the data for the calendar year 2023). 

g highincome_WB=0 

replace highincome_WB=1 if bpld==	30060	/*	Uruguay	*/
replace highincome_WB=1 if bpld==	70020	/*	New Zealand	*/
replace highincome_WB=1 if bpld==	50040	/*	Taiwan	*/
replace highincome_WB=1 if bpld==	42100	/*	France	*/
replace highincome_WB=1 if bpld==	40500	/*	Sweden	*/
replace highincome_WB=1 if bpld==	40100	/*	Finland	*/
replace highincome_WB=1 if bpld==	45213	/*	Czech Republic	*/
replace highincome_WB=1 if bpld==	41410	/*	Northern Ireland	*/
replace highincome_WB=1 if bpld==	45710	/*	Croatia	*/
replace highincome_WB=1 if bpld==	16010	/*	Bermuda	*/
replace highincome_WB=1 if bpld==	41300	/*	United Kingdom, ns	*/
replace highincome_WB=1 if bpld==	42500	/*	Netherlands	*/
replace highincome_WB=1 if bpld==	50200	/*	Korea	*/
replace highincome_WB=1 if bpld==	49900	/*	Europe, ns.	*/
replace highincome_WB=1 if bpld==	45300	/*	Germany	*/
replace highincome_WB=1 if bpld==	43610	/*	Azores	*/
replace highincome_WB=1 if bpld==	71047	/*	Northern Mariana Islands	*/
replace highincome_WB=1 if bpld==	26043	/*	Bahamas	*/
replace highincome_WB=1 if bpld==	46100	/*	Latvia	*/
replace highincome_WB=1 if bpld==	41400	/*	Ireland	*/
replace highincome_WB=1 if bpld==	50100	/*	Japan	*/
replace highincome_WB=1 if bpld==	26060	/*	Trinidad and Tobago	*/
replace highincome_WB=1 if bpld==	41000	/*	England	*/
replace highincome_WB=1 if bpld==	45500	/*	Poland	*/
replace highincome_WB=1 if bpld==	41100	/*	Scotland	*/
replace highincome_WB=1 if bpld==	30020	/*	Chile	*/
replace highincome_WB=1 if bpld==	26057	/*	St. Kitts-Nevis	*/
replace highincome_WB=1 if bpld==	46200	/*	Lithuania	*/
replace highincome_WB=1 if bpld==	30040	/*	Guyana/British Guiana	*/
replace highincome_WB=1 if bpld==	43600	/*	Portugal	*/
replace highincome_WB=1 if bpld==	29900	/*	Americas, ns	*/
replace highincome_WB=1 if bpld==	53400	/*	Israel/Palestine	*/
replace highincome_WB=1 if bpld==	54300	/*	United Arab Emirates	*/
replace highincome_WB=1 if bpld==	45600	/*	Romania	*/
replace highincome_WB=1 if bpld==	15000	/*	Canada	*/
replace highincome_WB=1 if bpld==	40400	/*	Norway	*/
replace highincome_WB=1 if bpld==	43300	/*	Greece	*/
replace highincome_WB=1 if bpld==	40000	/*	Denmark	*/
replace highincome_WB=1 if bpld==	45400	/*	Hungary	*/
replace highincome_WB=1 if bpld==	45212	/*	Slovakia	*/
replace highincome_WB=1 if bpld==	42000	/*	Belgium	*/
replace highincome_WB=1 if bpld==	26044	/*	Barbados	*/
replace highincome_WB=1 if bpld==	43400	/*	Italy	*/
replace highincome_WB=1 if bpld==	42600	/*	Switzerland	*/
replace highincome_WB=1 if bpld==	43800	/*	Spain	*/
replace highincome_WB=1 if bpld==	21070	/*	Panama	*/
replace highincome_WB=1 if bpld==	45000	/*	Austria	*/
replace highincome_WB=1 if bpld==	50010	/*	Hong Kong	*/
replace highincome_WB=1 if bpld==	40200	/*	Iceland	*/
replace highincome_WB=1 if bpld==	70010	/*	Australia	*/
replace highincome_WB=1 if bpld==	54000	/*	Saudi Arabia	*/
replace highincome_WB=1 if bpld==	26042	/*	Antigua-Barbuda	*/
replace highincome_WB=1 if bpld==	45200	/*	Czechoslovakia	*/
replace highincome_WB=1 if bpld==	53600	/*	Kuwait	*/

*keep year multyear sample serial cbserial numprec subsamp cluster strata  pernum perwt sploc related sex age marst birthyr marrno marrinyr yrmarr bpl bpld yrimmig yrsusa1 educ educd birthqtr ancestr1 language speakeng
 
    egen idh= group( multyear  serial cbserial cluster strata)
	gen foreign=bpl>=150 
	
*keep household with a least one migrant
	bys idh : egen hh_foreign= max(foreign)
	keep if hh_foreign==1
		

* age of migration
gen agemig=age-yrsusa
replace agemig=. if (foreign==0|yrimmig==0)
label var agemig "age when migrated"

* age last marriage
gen agemarriage =age- (multyear-yrmarr)
replace agemarriage =. if yrmarr==0 
label var agemarriage  "age when last married"


* male
	g male= sex==1 
	g male_migrant   = male==1 & foreign==1 
	g female_migrant = male==0 & foreign==1 

	
 * Countries of origin (Not cuba, no west indies)	 
g dev_ori =  inlist(bpl, 500  ,521 ,  600  ,200 ,210,  300)
replace dev_ori= 1 if inrange(bpl,511,519)
replace dev_ori=1 if ///
		  bpl==520 /*Afghanistan*/ | ///
          bpl==522 /*Iran*/ | ///
          bpl==524 /*Nepal*/ | ///
          bpl==532 /*Iraq*/ | ///
          bpl==533 /*Iraq/Saudi Arabia*/ | ///
          bpl==535 /*Jordan*/ | ///
          bpl==541 /*Syria*/ | ///
          bpl==542 /*Turkey*/ | ///
          bpl==544 /*Yemen Arab Republic (North)*/ | ///
          bpl==545 /*Yemen, PDR (South)*/ | ///
          bpl==547 /*Middle East, ns*/  

*MAIN ORIGIN COUNTRY 
	g NorthAfrica= inrange(bpld , 60011, 60019)
	g country_ori = 1 if bpl == 200  
	replace country_ori = 2 if  bpl == 521
	replace country_ori = 3 if  bpl == 500
	replace country_ori = 4 if  bpl ==   515 
	replace country_ori = 5 if NorthAfrica==1 | inlist( bpl,  520, 522,532, 535, 541, 542, 544)
	replace country_ori = 6 if NorthAfrica==0 &  bpl == 600
	replace country_ori = 7 if  bpl == 210|  bpl ==  300
	replace country_ori = 8  if inlist(bpl,511,512,513,514,516,517,518)
	
	label def ctrori 1 "Mexico" 2 "India" 3 "China" 4 "Philippines" 5 "MENA"  6 "SSA"  7  "Cent.South Amer"  8 "Rest.SE.Asia" , modify 
	label values country_ori  ctrori

		  
		  

		  
*** CODE EDUCATION 


g yedu=.
g edattaind=.
g us2020a_educ= educd

replace yedu=	0	if us2020a_educ==	2
replace yedu=	0	if us2020a_educ==	11
replace yedu=	0	if us2020a_educ==	12
replace yedu=	1	if us2020a_educ==	14
replace yedu=	2	if us2020a_educ==	15
replace yedu=	3	if us2020a_educ==	16
replace yedu=	4	if us2020a_educ==	17
replace yedu=	5	if us2020a_educ==	22
replace yedu=	6	if us2020a_educ==	23
replace yedu=	7	if us2020a_educ==	25
replace yedu=	8	if us2020a_educ==	26
replace yedu=	9	if us2020a_educ==	30
replace yedu=	10	if us2020a_educ==	40
replace yedu=	11	if us2020a_educ==	50
replace yedu=	12	if us2020a_educ==	61
replace yedu=	12	if us2020a_educ==	63
replace yedu=	12	if us2020a_educ==	64
replace yedu=	12	if us2020a_educ==	65
replace yedu=	13	if us2020a_educ==	71
replace yedu=	13	if us2020a_educ==	81
replace yedu=	13	if us2020a_educ==	101
replace yedu=	13	if us2020a_educ==	114
replace yedu=	13	if us2020a_educ==	115
replace yedu=	13	if us2020a_educ==	116

*elie adjust
replace yedu=	14	if us2020a_educ==	81
replace yedu=	16	if us2020a_educ==	101
replace yedu=	18	if us2020a_educ==	114
replace yedu=	18	if us2020a_educ==	115
replace yedu=	18	if us2020a_educ==	116 





replace edattaind=	110	if us2020a_educ==	2
replace edattaind=	110	if us2020a_educ==	11
replace edattaind=	110	if us2020a_educ==	12
replace edattaind=	120	if us2020a_educ==	14
replace edattaind=	120	if us2020a_educ==	15
replace edattaind=	120	if us2020a_educ==	16
replace edattaind=	120	if us2020a_educ==	17
replace edattaind=	120	if us2020a_educ==	22
replace edattaind=	212	if us2020a_educ==	23
replace edattaind=	212	if us2020a_educ==	25
replace edattaind=	212	if us2020a_educ==	26
replace edattaind=	221	if us2020a_educ==	30
replace edattaind=	221	if us2020a_educ==	40
replace edattaind=	221	if us2020a_educ==	50
replace edattaind=	311	if us2020a_educ==	61
replace edattaind=	311	if us2020a_educ==	63
replace edattaind=	311	if us2020a_educ==	64
replace edattaind=	312	if us2020a_educ==	65
replace edattaind=	312	if us2020a_educ==	71
replace edattaind=	312	if us2020a_educ==	81
replace edattaind=	400	if us2020a_educ==	101
replace edattaind=	400	if us2020a_educ==	114
replace edattaind=	400	if us2020a_educ==	115
replace edattaind=	400	if us2020a_educ==	116

label def edattaind_lbl /// 
		  0  "NIU (not in universe)" /// 
         100 "Less than primary completed (n.s.)" /// 
         110 "No schooling"  /// 
         120 "Some primary completed" /// 
         130 "Primary (4 yrs) completed" /// 
         211 "Primary (5 yrs) completed" /// 
         212 "Primary (6 yrs) completed" /// 
         221 "Lower secondary general completed" /// 
         222 "Lower secondary technical completed" /// 
         311 "Secondary, general track completed" /// 
         312 "Some college completed" /// 
         320 "Secondary or post-secondary technical completed" /// 
         321 "Secondary, technical track completed" /// 
         322 "Post-secondary technical education" /// 
         400 "University completed" ///  
         999 "Unknown/missing", modify
		 
label values edattaind  edattaind_lbl  	



	  
		  

** married before or after migration: timing of last mariage 

	gen yearsmarriedpostmig=yrmarr-yrimmig
	replace yearsmarriedpostmig=. if yrmarr==0|yrimmig==0
	gen sameyear_marriedmig=yearsmarriedpostmig==0 if yearsmarriedpostmig~=.

   
  *When did ever-married migrants LAST marry
	g       status_evermarried = 1 if  cond(yearsmarriedpostmig<0,1,0) &  yearsmarriedpostmig~=. 
	replace status_evermarried = 2 if   yearsmarriedpostmig==0 & yearsmarriedpostmig~=.
	replace status_evermarried = 3 if  cond(yearsmarriedpostmig>0,1,0) & yearsmarriedpostmig~=.
	label def evermar 1 "Married before migrating " 2 "Married same year as migrating " 3 "Married after migrating" , modify 
	label values status_evermarried  evermar 
	label var status_evermarried "When did ever-married migrants last marry"
	
 * Current Marital Status of migrants aged 18+ at time of migration

	gen evermarried=marrno>=1 & marrno<=3

** Create more detailed category

	** generate categories
	* Never married
	gen expandedmarstat=1 if marst==6
	* Married only once and married with spouse present
	 * And married before moving
	replace expandedmarstat=2 if marst==1 & marrno==1 & status_evermarried ==1
	* married in year of move
	replace expandedmarstat=3 if marst==1 & marrno==1 & status_evermarried ==2
	* married after move
	replace expandedmarstat=4 if marst==1 & marrno==1 & status_evermarried ==3
	* Married only once and married with spouse absent
	 * And married before moving
	replace expandedmarstat=5 if marst==2 & marrno==1 & status_evermarried ==1
	* married in year of move
	replace expandedmarstat=6 if marst==2 & marrno==1 & status_evermarried ==2
	* married after move
	replace expandedmarstat=7 if marst==2 & marrno==1 &  status_evermarried ==3
	* Separated or Divorced and married only once
	 * And married before moving
	replace expandedmarstat=8 if (marst==3|marst==4) & marrno==1 & status_evermarried ==1
	* married in year of move
	replace expandedmarstat=9 if (marst==3|marst==4)  & status_evermarried ==2
	* married after move
	replace expandedmarstat=10 if (marst==3|marst==4)  &  status_evermarried ==3
	* Widowed and married only once
	replace expandedmarstat=11 if marst==5
	* Married multiple times
	replace expandedmarstat=12 if marrno==2|marrno==3

label define martype 1 "Never married" 2 "Married before moving, spouse present" 3 "Married same year, spouse present" 4 "Married after moving, spouse present" 5 "Married before moving, spouse absent" 6 "Married same year, spouse absent" 7 "Married after moving, spouse absent" 8 "Married before moving, Separated or Divorced"  9 "Married same year, Separated or Divorced" 10 "Married after moving, Separated or Divorced" 11 "Widowed" 12 "Married multiple times", modify 
label values expandedmarstat martype
 
 
 * ELIE CODE: only for evermarried, nomatter whether they are married multiple times 
 
	g evermarr_det= .
	replace evermarr_det = 1 if marst==1  & status_evermarried ==1
	replace evermarr_det = 2 if marst==2  & status_evermarried ==1
	replace evermarr_det = 3 if inlist(marst,3,4)  & status_evermarried ==1
	
	replace evermarr_det = 4 if marst==1  & status_evermarried ==2
	replace evermarr_det = 5 if marst==2  & status_evermarried ==2
	replace evermarr_det = 6 if inlist(marst,3,4)  & status_evermarried ==2
  
	replace evermarr_det = 7 if marst==1  & status_evermarried ==3
	replace evermarr_det = 8 if marst==2  & status_evermarried ==3
	replace evermarr_det = 9 if inlist(marst,3,4)  & status_evermarried ==3
  
	replace evermarr_det = 10 if marst==5  
  
  
label define everdet  1 "Spouse present, mar bef mig" 2 "Spouse absent, mar bef mig"  3 "Separated or Divorced, mar bef mig" ///
							4 "Spouse present, mar same y" 5 "Spouse absent, mar same y"  6 "Separated or Divorced, mar same y"   ///
							7 "Spouse present, mar aft mig" 8 "Spouse absent, mar aft mig"  9 "Separated or Divorced, mar aft mig" ///
							10 "widowed", modify 
							
	label values evermarr_det  everdet
 
 
	
 
 
 *  marriage same year as migration : coded as do not know	
  
   g married_beforemig = cond(yearsmarriedpostmig<0,1,0) if yearsmarriedpostmig~=. 
   g married_aftermig  = cond(yearsmarriedpostmig>=0,1,0) if yearsmarriedpostmig~=. 
   g married_strictaftermig  = cond(yearsmarriedpostmig>0,1,0) if yearsmarriedpostmig~=. 
	
  replace married_beforemig=0 if marst==6  /* includes people that divorced just before migration*/
  label var 	married_beforemig "married  before migrating"

	label var  sameyear_marriedmig "married same year as migrated"  
	label var  married_strictaftermig "married after migration (strictly)"


  
 * In case of two marriage and still married at time of survey
	
	*(i) last time married was after mig ( married_aftermig ==1)
	   *case a) first marriage occured after mig                  -> change in marital status from unmarried to married 
	   *case b) first marriage occured before mig and divorced after:  change in marital status from married to unmarried 
	 
	*(ii) last time married was before migration : no change in marital status nor spouse 
 
 * In case of two marriage and separated at time of survey
	*(i) last time married was after mig ( married_aftermig ==1)
		*case c) first marriage occured after mig -> change in marital status  from unmarried to married 
		*case d) first marriage occured before mig and got seprated after -> change in marital status from married to unmarried 
	*(ii) last time married was before migration :  change in marital status  
 
 
	g        predict_mars_beforemig = 1 if married_beforemig==1  
	replace  predict_mars_beforemig = 1 if inlist(marrno,2,3) &   married_aftermig==1 /*assumption 1: first marriage occured before migration  */
	replace  predict_mars_beforemig = 0 if marst==6  
	replace  predict_mars_beforemig = 0 if marrno==1 & married_aftermig==1   
	label def ppp 1 "married before migrating" 0 "not married before migrating", modify 
	label values predict_mars_beforemig  ppp 
	
	g        predict_mars_beforemig_v2 =  predict_mars_beforemig 
	replace  predict_mars_beforemig_v2 = 0 if inlist(marrno,2,3) &   married_aftermig==1  /*assumption 2: first marriage occured after migration  */
		
	g        predict_mars_beforemig_v3 =  predict_mars_beforemig 
	replace  predict_mars_beforemig_v3 = . if inlist(marrno,2,3) &   married_aftermig==1  /*ass3:missing married twice*/

	label var  predict_mars_beforemig     "married before mig (ass1 for mult.mar.: 1st was bef mig)"
	label var  predict_mars_beforemig_v2  "married before mig (ass2 for mult.mar.: 1st was aft mig)"
	label var  predict_mars_beforemig_v3   "married before mig (ass 3 mult.mar.: missing)"
	
	
 
 * Change marital status after migration : from married to separated (at least once)
 
   g       change_married_to_unmarried = .
  replace  change_married_to_unmarried  = 0 if  marrno ==1 & married_beforemig==1 & inlist(marst,1,2)
  replace  change_married_to_unmarried  = 1 if  marrno ==1 & married_beforemig==1 & inlist(marst,3,4,5) /*assume that people get divorced after mig and not before*/
	
	*for people married twice of more 
    replace  change_married_to_unmarried  = 0 if inlist(marrno,2,3) &  married_beforemig==1 & inlist(marst,1,2)    /*last time married was before migration and still married*/  
    replace  change_married_to_unmarried  = 1 if inlist(marrno,2,3) &  married_beforemig==1 & inlist(marst,3,4,5)  /*last time married was before migration and now separated*/  

	*assumption to have a upper bound of change from married tu unmarried :
			*first marriage occured before mig and divorced after mig
    replace   change_married_to_unmarried = 1 if inlist(marrno,2,3) &   married_aftermig==1 &  inlist(marst,1,2,3,4,5)  /*last time married was after migrationa and married twice*/  
	
	*assumption to have a lower bound of change from married tu unmarried :
			*first marriage occured after migration 
    g         change_married_to_unmarried_v2 =  change_married_to_unmarried  
	replace   change_married_to_unmarried_v2  =  . if inlist(marrno,2,3) &   married_aftermig==1 &  inlist(marst,1,2,3,4,5)  /*last time married was after migrationa and married twice*/  
	
	g change_married_to_unmarried_v3= change_married_to_unmarried_v2 
	
 
 * Change marital status after migration : from unmarried to married (at least one)

	g        change_unmarried_to_married  = .
	replace  change_unmarried_to_married  = 0 if  marst==6 
	replace  change_unmarried_to_married  = 1 if  marrno ==1  & married_strictaftermig ==1  /*stricly after migration*/
	
	g       change_unmarried_to_married_v2 = change_unmarried_to_married
	replace change_unmarried_to_married_v2 = 1  if inlist(marrno,2,3) &   married_strictaftermig ==1 &  inlist(marst,1,2,3,4,5)  /*last time married was after migrationa and married twice*/   
	
	g change_unmarried_to_married_v3= change_unmarried_to_married 
	
	/*g       change_mar  = change_married_to_unmarried  if  predict_mars_beforemig==1 
	replace change_mar  = change_unmarried_to_married   if  predict_mars_beforemig==0 
	g       change_mar_v2  = change_married_to_unmarried_v2  if  predict_mars_beforemig_v2 ==1 
	replace change_mar_v2 = change_unmarried_to_married_v2    if  predict_mars_beforemig_v2 ==0 
	*/
	
	label var change_married_to_unmarried      "got separated cond.on being married bef mig (ass1 ) "
	label var  change_unmarried_to_married     "got married  cond.on being unmarried bef mig (ass1 )"
	
	label var change_married_to_unmarried_v2   "got separated cond.on being married bef mig(ass2) "
    label var  change_unmarried_to_married_v2  "got married cond.on being unmarried bef mig (ass2) "

	
	label var change_married_to_unmarried_v3   "got separated cond.on being married bef mig(ass3) "
    label var  change_unmarried_to_married_v3  "got married cond.on being unmarried bef mig (ass3) "

	
	
	
	*married twice 
	
	g marriedtwice= inrange(marrno,2,3)
	label var marriedtwice "married twice or more"
	
	
	g       marstatus_if_married_befmig = 1 if marst==1 & change_married_to_unmarried ==0 
	replace marstatus_if_married_befmig = 2 if marst==2 & change_married_to_unmarried ==0 
	replace marstatus_if_married_befmig = 3 if change_married_to_unmarried ==1
	label def bbb 1 "married spouse present" 2 "married spouse absent" 3 "separated once after migration", modify 
	label values marstatus_if_married_befmig bbb 
	label var  marstatus_if_married_befmig "status cond.on married bef mig (ass1)"
	
	g       marstatus_if_married_befmig_v2 = 1 if marst==1 & change_married_to_unmarried_v2 ==0 
	replace marstatus_if_married_befmig_v2 = 2 if marst==2 & change_married_to_unmarried_v2 ==0 
	replace marstatus_if_married_befmig_v2 = 3 if change_married_to_unmarried_v2 ==1
	label values marstatus_if_married_befmig_v2 bbb 
	label var  marstatus_if_married_befmig_v2 "status cond.on married bef mig (ass2)"
		
	g       marstatus_if_married_befmig_v3 = 1 if marst==1 & change_married_to_unmarried_v3 ==0 
	replace marstatus_if_married_befmig_v3 = 2 if marst==2 & change_married_to_unmarried_v3 ==0 
	replace marstatus_if_married_befmig_v3 = 3 if change_married_to_unmarried_v3 ==1
	label values marstatus_if_married_befmig_v3 bbb 
	label var  marstatus_if_married_befmig_v3 "status cond.on married bef mig (ass3)"
	
	
	
		
*  Information about SPOUSE 

	*cap drop apl 
	*do "/Users/eliemurard/Dropbox/Migration, families, and counterfactual families/Do files/ACS/Foreign ancestry coding -Simone.do"
	
    g spouse_present= 0 
	g spouse_age = .
	g spouse_yrimmig=.
	g spouse_marst=. 
	g spouse_bpld =. 
	g spouse_bpl =. 
	g spouse_yrmarr =.
	g spouse_yrsusa1 =.
	
	g spouse_yedu =.
	g spouse_edattaind =.
	
	
	
	g spouse_language=. 
	g spouse_ancestr1=.
	g spouse_speakeng=.
	
	 
	

quietly{
levelsof pernum  , local(levels) 
	foreach l of local levels {
		tempvar t
		tempvar hhsp
		tempvar spmig
		 g `t'= sploc if pernum== `l'  
		 bys idh: egen `hhsp'= max(`t')
		 bys idh: egen  `spmig'= max( (pernum==`hhsp')   ) 
		
		replace spouse_present = 1 if `spmig'==1 & pernum== `l'  
		
		foreach v in age yrimmig marst bpl  bpld yrmarr yrsusa1 yedu edattaind ancestr1 language speakeng {
			
			tempvar y 
			tempvar spyy
			g `y' = `v'  if   (pernum==`hhsp') 
			bys  idh:  egen  `spyy'= max(`y') 
			replace spouse_`v'   =  `spyy' if `spmig'==1 & pernum== `l'  
			
		}
	}
}
*		
		*label values spouse_apl     bpld_lbl
		*label values spouse_ancestr1 ancestr1_lbl
		
		label values spouse_edattaind  edattaind_lbl  	
		
* Spouse characteristics and relative timmig marriage and migration 

		g 	married_spouse_present= marst==1 
		label var married_spouse_present "married with spouse present "

		g spouse_samebpld = bpld==spouse_bpld if marst==1 
		
		g spouse_orig_st= 1 if   bpld==spouse_bpld & marst==1 
		replace spouse_orig_st=2 if inrange(spouse_bpl,0,149) & marst==1 
		replace spouse_orig_st= 3 if  spouse_orig_st!=1 & spouse_orig_st!=2 & marst==1 
		label def oo 1 "same birth country"  2 "US born" 3 "born other country ", modify 
		label values spouse_orig_st oo 
		
		
		*g spouse_same_ances= apl==spouse_apl if marst==1  &  spouse_apl!= . & apl!= .
		
		
		
		*sanity check: OK 
		g spouse_sameyrmarr = yrmarr==spouse_yrmarr if marst==1 
		
		gen spouse_diff = spouse_yrimmig - spouse_yrmarr  if marst==1 
		replace spouse_diff =. if  spouse_yrimmig==0|spouse_yrmarr ==0
		g spouse_timing_marr = 1      if spouse_diff >0   & spouse_diff!=.
		replace spouse_timing_marr=2  if spouse_diff <0  & spouse_diff!=.
		replace spouse_timing_marr=3  if spouse_diff ==0 & spouse_diff!=.
		replace  spouse_timing_marr=. if spouse_sameyrmarr!=1 
		label def ss 1 "married before mig"  2 "married after mig" 3 "same year ", modify 
		label values spouse_timing_marr  ss
		
		ta spouse_timing_marr  predict_mars_beforemig  if male_migrant   ==1 & marst==1 & spouse_samebpl ==1,col

		
		
*education 
	
	g       highestedu = 1 if inrange(educd,0,22) /*grade 5 or less */
	replace highestedu = 2 if inrange(educd,23,61) /*grade 6-12 school */
	replace highestedu = 3 if inrange(educd,63,81) /*high school diploma */ 
	replace highestedu = 4 if inlist(educd,101) /* Bachelor's degree*/ 
	replace highestedu = 5 if inrange(educd,114,116) /*  Master's degree or more */ 
	label def edd 1 "grade 5 or less" 2 "grade 6-12"  3 "high school diploma" 4 "Bachelor diploma" 5 "Master or more", modify 
	label values highestedu edd
	label var  highestedu  "highest educational attainment"
	
	g masterdegree= highestedu == 5
	g bachelordegree= inlist(highestedu,4,5) 
	g highschool= inlist(highestedu,3,4,5) 
	label var masterdegree   "Highest education: master or more"
	label var bachelordegree "Highest education: bachelor or more"
	label var highschool  "Highest education: high school or more"

	
	label values spouse_language language_lbl 
	label values spouse_ancestr1 ancestr1_lbl
	
	cap drop __* 

	
***********************************************************************************************************************	*						

* BUILD FINAL SAMPLE

************************************************************************************************************************			
	




* SAMPLE: foreign born , 18-60, migrated when 18-40 

	keep if bpl>=149 
	
	keep if age>=18 & age<=60
	
	* KEEP ONLY MIGRANTS FROM NON HIGH-INCOME COUNTRY ACOORDING TO WORLD BANK
	
	drop if highincome_WB==1
	
	keep if inrange(agemig, 18,40)
		
	
	*Ancestry husband wife 
	
	
	*g spouse_ances_eq_bpld_hus = spouse_apl==bpld  if marst==1  &  spouse_apl!= . & bpld!= .
	*label var spouse_ances_eq_bpld_hus "spouse same ancestry as husb birthplace" 	
		
		
	* Birthplace spouse 
	
	g 	    sp_bpl_ev1_ = .
	g 	    sp_bpl_ev2_ = .
	g 	    sp_bpl_ev3_ = .
	
	replace sp_bpl_ev1_ = 1 if spouse_orig_st==1 & marst==1  & status_evermarried ==1
	replace sp_bpl_ev1_ = 2 if spouse_orig_st==2 & marst==1  & status_evermarried ==1
	replace sp_bpl_ev1_ = 3 if spouse_orig_st==3 & marst==1  & status_evermarried ==1
	
	replace sp_bpl_ev2_ = 4 if spouse_orig_st==1 & marst==1  & status_evermarried ==2
	replace sp_bpl_ev2_=  5 if spouse_orig_st==2 & marst==1  & status_evermarried ==2
	replace sp_bpl_ev2_ = 6 if spouse_orig_st==3 & marst==1  & status_evermarried ==2
  
	replace sp_bpl_ev3_ = 7 if spouse_orig_st==1 & marst==1  & status_evermarried ==3
	replace sp_bpl_ev3_ = 8 if spouse_orig_st==2 & marst==1  & status_evermarried ==3
	replace sp_bpl_ev3_ = 9 if spouse_orig_st==3 & marst==1  & status_evermarried ==3
	
	*replace sp_present_bpl=999  if inlist(evermarr_det,2,3,5,6,8,9)
	
	label define sporigin  	1 "Spouse born in the same country, mar bef mig" 2 "Spouse born in the U.S, mar bef mig"  3 "Spouse born in another country, mar bef mig" ///
						4 "Spouse born in the same country, mar same y" 5 "Spouse born in the U.S, mar same y"  6 "Spouse born in another country,, mar same y"   ///
						7 "Spouse born in the same country, mar aft mig" 8 "Spouse born in the U.S, mar aft mig"  9 "Spouse born in another country,, mar aft mig" , modify 
							
	*label values sp_present_bpl  sporigin  
	label values  sp_bpl_ev1_  sporigin  
	label values  sp_bpl_ev2_  sporigin  
	label values  sp_bpl_ev3_  sporigin  
	
	
	
	label var male "male"
	
	foreach v of  varlist status_evermarried  spouse_orig_st   expandedmarstat evermarr_det sp_bpl_ev1_ sp_bpl_ev2_ sp_bpl_ev3_  marstatus_if_married_befmig marstatus_if_married_befmig_v2 marstatus_if_married_befmig_v3  {
	local l: variable label  `v'
	levelsof `v'
	foreach value in `r(levels)' {
		cap drop `v'`value'
		g `v'`value' = `v'==`value'  if `v'!=.
		local d: label(`v') `value' 
		label var `v'`value' "`l': `d'"	
	}
	}
	* 
	
	*Change ref category: ever-married migrants for the variable:
	foreach v in expandedmarstat2 expandedmarstat5 expandedmarstat8 expandedmarstat3 expandedmarstat6 ///
				 expandedmarstat9 expandedmarstat4 expandedmarstat7 expandedmarstat10 expandedmarstat11 expandedmarstat12 {
		
		replace `v'=. if evermarried==0 
	}
	
	
	
	
	
	
*agemig 
	g agemig_male= agemig if male==1 
	g agemig_fem= agemig  if male==0 
	label var agemig "Age of migration"
	label var agemig_male "Age of migration for male"
	label var agemig_fem "Age of migration for female"
	
	foreach v of varlist agemig  {
		local l: variable label  `v'
			bys country_ori: egen `v'_p50=   pctile(`v') , p(50)
			bys country_ori: egen `v'_p75=   pctile(`v') , p(75)
			bys country_ori: egen `v'_p25=   pctile(`v') , p(25)
			bys country_ori: egen `v'_p10=   pctile(`v') , p(10)
			bys country_ori: egen `v'_p90=   pctile(`v') , p(90)
			
			label var `v'_p50 "`l': median"	
			label var `v'_p75 "`l': 75 pctile"	
			label var `v'_p25 "`l': 25 pctile"	
			label var `v'_p10 "`l': 10 pctile"	
			label var `v'_p90 "`l': 90 pctile"			
	}
	
	bys country_ori male : egen  agemig_p50_bygender=   pctile( agemig) , p(50)
	label var agemig_p50_bygender "Age of migration: median"
	
* Time in the US 


 	bys country_ori: egen  yrsusa1_p50=   pctile(yrsusa1 ) , p(50)
	label var yrsusa1_p50 "Years in the United States, median"
	bys country_ori male: egen  yrsusa1_p50_bygender=   pctile(yrsusa1 ) , p(50)
	label var yrsusa1_p50_bygender "Years in the United States, median"

	
	label var evermarried "Ever-married"
	
	
	label var expandedmarstat1 "Never married"
	label var  status_evermarried1 "Married before migrating"
	label var status_evermarried2 "Married same year as migrating"
	label var status_evermarried3 "Married after migrating"	
	label var expandedmarstat11 "Widowed" 
	label var expandedmarstat12   "Married multiple times"


	
	label var evermarr_det1  "Spouse present, mar bef mig"
	label var  evermarr_det2  "Spouse absent, mar bef mig"
	label var evermarr_det3 "Separated/Divorced, mar bef mig"
	label var evermarr_det4 "Spouse present, mar same year as mig"
	label var evermarr_det5 "Spouse absent, mar same year as mig "
	label var evermarr_det6 "Separated/Divorced, mar same year as mig"
	label var evermarr_det7 "Spouse present, mar aft mig"
	label var evermarr_det8 "Spouse absent, mar aft mig"
	label var evermarr_det9 "Separated or Divorced, mar aft mig "

	label var sp_bpl_ev1_1 "Spouse born in the same country, mar bef mig"
	label var sp_bpl_ev1_2 "Spouse born in the U.S, mar bef mig"
	label var sp_bpl_ev2_4 "Spouse born in the same country, mar same year as mig"
	label var sp_bpl_ev2_5 "Spouse born in the U.S, mar same year as mig"
	label var sp_bpl_ev3_7 "Spouse born in the same country, mar aft mig"
	label var sp_bpl_ev3_8 "Spouse born in the U.S, mar aft mig"

 
***
	
	keep year multyear sample serial sex age birthqtr marst birthyr marrno yrmarr evermarr* country* *ori*  expanded* status_evermarried*  sp_bpl* agemig*  agemig_p50_bygender  masterdegree bachelordegree highschool   expandedmarstat1   bpl bpld yrimmig yrsusa1 educ educd male change* spouse*  *edu* 
	
					
save  "${data}ACS2017-2021_finalready.dta", replace
